# coding : UTF-8
import threading
import re
import time
import urllib
import urllib.request
import string

def get_image_id(page):
    url = "http://www.netbian.com/meinv/index_" + str(page)+ ".htm"
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
    request = urllib.request.Request(url=url, headers=headers)
    html = urllib.request.urlopen(request).read()
    html = html.decode('gbk')
    items = re.findall(re.compile(r'href="/desk/(\d*)\.htm">'), html)
    #for item in items:
    #    print("下载第%d页图片编号%s..." %(page ,item))
    return items

def get_imge(user_id):
    url = "http://www.netbian.com/desk/" + str(user_id)+ "-1920x1080.htm"
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
    request = urllib.request.Request(url=url, headers=headers)
    html = urllib.request.urlopen(request).read()
    html = html.decode('gbk')
    items = re.findall(re.compile(r'<tr><td align="left">\n<a href="(.+?)" title="'), html)
    if items == list():
        print("无内容...")
        return
    #print(items[0])
    print("下载 %s.png..."  %user_id)
    urllib.request.urlretrieve(items[0], './download/%s.png' %user_id)

if __name__ == '__main__':
    for i in range(1,190):
        print("page %d" %i)
        values = get_image_id(i)
        for value in values:
            get_imge(value)

